package com.imooc.miaosha.pojo;

import com.sun.org.apache.bcel.internal.classfile.Code;

import java.io.Serializable;


public class ApiResultTo<T> {
    private static final long serialVersionUID = 4785961935752148785L;

    private boolean success;
    private Integer errorCode;
    private String message="";//尽量不要返回null，这是一个很小的技巧，
    private T data;
    public static <K> ApiResultTo<K> buildSuccess(CodeMsg codeMsg){
        ApiResultTo<K> result = new ApiResultTo<>();
        result.setSuccess(true);
        result.setErrorCode(codeMsg.getCode());
        result.setMessage(codeMsg.getMessage());
        return result;
    }
    public static <K> ApiResultTo<K> buildSuccess(K data){
        ApiResultTo<K> result = new ApiResultTo<>();
        result.setSuccess(true);
        result.setErrorCode(0);
        result.setData(data);
        return result;
    }
    public static <K> ApiResultTo<K> buildSuccess(K data, String message){
        ApiResultTo<K> result = new ApiResultTo<>();
        result.setSuccess(true);
        result.setErrorCode(0);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
    public static ApiResultTo<?> buildFailed(Integer code, String message){
        ApiResultTo<?> result = new ApiResultTo<>();
        result.setSuccess(false);
        result.setErrorCode(code);
        result.setMessage(message);
        return result;
    }
    public static ApiResultTo<?> buildFailed(CodeMsg codeMsg){
        ApiResultTo<?> result=new ApiResultTo<>();
        result.setSuccess(false);
        result.setErrorCode(codeMsg.getCode());
        result.setMessage(codeMsg.getMessage());
        return result;
    }
    public static ApiResultTo<?> buildFailed(CodeMsg codeMsg,String message){
        ApiResultTo<?> result = new ApiResultTo<>();
        result.setSuccess(false);
        result.setErrorCode(codeMsg.getCode());
        result.setMessage(message);
        return result;
    }


    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public boolean isSuccess() {
        return success;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
